package edu.yang.dbutils.result;

import edu.yang.dbutils.ResultSetHandler;
import edu.yang.dbutils.RowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author yang
 * @date 2024-05-12
 */
public class BeanHandler<T> implements ResultSetHandler<T> {
    private final Class<T> beanClass;

    public BeanHandler(Class<T> beanClass) {
        this.beanClass = beanClass;
    }

    /**
     * 将一条数据映射至实体对象中
     * @param rs 结果集对象
     * @return
     * @throws SQLException
     */
    @Override
    public T handle(ResultSet rs) throws SQLException {
        return rs.next() ? RowProcessor.toBean(rs, beanClass) : null;
    }
}
